package com.n.Internet.test;

import java.io.*;
import java.net.Socket;

/**
 * 客户端
 * localhost代表本机IP
 * Socket类设置了要连接的服务器端的主机名称与端口号，此时客户端
 * 与服务器端会建立一个Socket连接,并且实现数据通信
 *
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-27-15:03 15:03
 */
public class MyClient {
    public static void main(String[] args) throws IOException {
        Socket client = new Socket("localhost", 7777);

        //写数据（发送请求）
        PrintWriter request = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));

        request.println("chai");
        //清空、提交
        request.flush();
        request.println("第二次");
        request.flush();
        //读数据（接收响应）
        BufferedReader response = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));

        System.out.println(response.readLine());


        client.close();
    }
}
